﻿using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace UnityEditor.AddressableAssets
{
    public class AddressableAssetsPreprocessor : AssetPostprocessor
    {
        private const string PREFIX = "Assets/AddressableAssets/";
        private const string ROOT_GROUP = "RootGroup";
        private static readonly string[] ExtensionToSkip = {".lua",".txt",".bytes"};

        static List<string> FilterAssets(string[] assets)
        {
            List<string> results = new List<string>();
            foreach (var asset in assets)
            {
                if (asset.StartsWith(PREFIX) && Path.HasExtension(asset))
                {
                    if (ExtensionToSkip != null)
                    {
                        var extension = Path.GetExtension(asset);
                        if (ExtensionToSkip.Contains(extension))
                            continue;
                    }
                        
                    results.Add(asset);
                }
            }

            return results;
        }

        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            if (AddressableUtil.DefaultGroupName == null)
                return;

            var assetsToImport = FilterAssets(importedAssets);

            if (assetsToImport.Count > 0)
            {
                foreach (var asset in assetsToImport)
                {
                    string guid = null;
                    if (!AddressableUtil.GetGUIDFromPath(asset, ref guid))
                        continue;
                    getGroupAndAddress(asset, out var group, out var address);
                    AddressableUtil.AddAssetToAddressableGroup(asset, address, group,true);
                }
            }

            var assetsToDelete = FilterAssets(deletedAssets);
            if (assetsToDelete.Count > 0)
            {
                foreach (var asset in assetsToDelete)
                {
                    AddressableUtil.RemoveAssetFromAddressable(asset);
                }
            }

            for (int i = 0; i < movedAssets.Length; i++)
            {
                var asset = movedAssets[i];
                var from = movedFromAssetPaths[i];
                if (from.StartsWith(PREFIX))
                {
                    if (asset.StartsWith(PREFIX))
                    {
                        AddressableUtil.RemoveAssetFromAddressable(asset);
                        getGroupAndAddress(asset, out var group, out var address);
                        AddressableUtil.AddAssetToAddressableGroup(asset, address, group, true);
                    }
                    else
                        AddressableUtil.RemoveAssetFromAddressable(asset);
                }else if (asset.StartsWith(PREFIX))
                {
                    getGroupAndAddress(asset, out var group, out var address);
                    AddressableUtil.AddAssetToAddressableGroup(asset, address, group, true);
                }
            }
        }

        private static void getGroupAndAddress(string assetPath, out string group, out string address)
        {
            var nonPrefix = assetPath.Substring(PREFIX.Length);
            group = nonPrefix.Contains("/") ? nonPrefix.Substring(0, nonPrefix.IndexOf("/")) : ROOT_GROUP;
            address = nonPrefix;
        }
    }
}